package com.bfly.mall.admin.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.admin.entity.Menu;
import com.bfly.mall.admin.dto.MenuFormDTO;
import com.bfly.mall.admin.dto.MenuListDTO;
import com.bfly.mall.admin.service.IMenuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 系统菜单
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/16 13:28
 */
@RestController
@RequestMapping(value = "/manage/menu")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;

    /**
     * 菜单列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/16 22:02
     */
    @GetMapping(value = {"/list", "/all"})
    @PermissionCheck(required = false)
    public void list(HttpServletResponse response) {
        Map<String, Object> queryMap = new HashMap<>(1);
        queryMap.put("parent_id", 0);
        Map<String, Boolean> sortMap = new HashMap<>(1);
        sortMap.put("seq", true);

        List<Menu> list = menuService.getList(queryMap, null, sortMap);
        List<MenuListDTO> menuListDTOS = new ArrayList<>();
        for (Menu menu : list) {
            MenuListDTO dto = filterEmptyChildren(menu);
            menuListDTOS.add(dto);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(menuListDTOS));
    }

    private MenuListDTO filterEmptyChildren(Menu menu) {
        List<Menu> list = menu.getChildren();
        List<MenuListDTO> listDTOS = null;
        if (CollectionUtils.isEmpty(list)) {
            menu.setChildren(null);
        } else {
            listDTOS = new ArrayList<>();
            for (Menu c : list) {
                MenuListDTO dto = filterEmptyChildren(c);
                listDTOS.add(dto);
            }
            Collections.sort(listDTOS);
        }
        MenuListDTO dto = new MenuListDTO();
        BeanUtils.copyProperties(menu, dto);
        dto.setChildren(listDTOS);
        return dto;
    }

    /**
     * 新增菜单
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/17 22:29
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增菜单")
    public void add(HttpServletResponse response, @Valid @RequestBody MenuFormDTO formDTO, BindingResult result) {
        validData(result);
        menuService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    @PostMapping(value = "/edit")
    @ActionModel(value = "修改菜单")
    public void edit(HttpServletResponse response, @Valid @RequestBody MenuFormDTO formDTO, BindingResult result) {
        validData(result);
        menuService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    @PostMapping(value = "/delete")
    @ActionModel(value = "删除菜单")
    public void delete(HttpServletResponse response, @RequestBody Integer... menuId) {
        menuService.remove(menuId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}